<script setup lang="ts">
import { computed, ref } from 'vue';

import { message } from 'ant-design-vue';

// 接口定义
interface Customer {
  id: string;
  name: string;
  petName: string;
  phone: string;
  memberLevel: 'gold' | 'normal';
  memberCard: string;
  balance: number;
  type: 'registered' | 'walkin';
}

interface Props {
  visible: boolean;
  customer: Customer | null;
}

interface Emits {
  (e: 'update:visible',value: boolean): void;
  (e: 'success',amount: number): void
}

const props = defineProps<Props>();
const emit = defineEmits<Emits>();

// 响应式数据
const rechargeAmount = ref<number>(0);
const paymentMethod = ref<string>('');
const operator = ref<string>('');
const remarks = ref<string>('');

// 计算属性
const newBalance = computed(() => {
  if (!props.customer) return 0;
  return props.customer.balance + rechargeAmount.value;
});

// 方法
const handleCancel = () => {
  emit('update:visible', false);
  resetForm();
};

const handleConfirm = () => {
  if (!props.customer) {
    message.warning('顾客信息不存在');
    return;
  }

  if (rechargeAmount.value <= 0) {
    message.warning('请输入有效的充值金额');
    return;
  }

  if (!paymentMethod.value) {
    message.warning('请选择支付方式');
    return;
  }

  if (!operator.value) {
    message.warning('请选择操作人员');
    return;
  }

  // 模拟充值成功
  message.success(`充值成功！充值金额: ¥${rechargeAmount.value}`);
  emit('success', rechargeAmount.value);
  emit('update:visible', false);
  resetForm();
};

const resetForm = () => {
  rechargeAmount.value = 0;
  paymentMethod.value = '';
  operator.value = '';
  remarks.value = '';
};
</script>

<template>
  <a-modal
    :visible="visible"
    title="会员充值"
    width="500px"
    @cancel="handleCancel"
    :footer="null"
  >
    <div class="recharge-modal">
      <!-- 顾客信息 -->
      <div class="customer-info">
        <div class="info-item">
          <span class="label">顾客姓名:</span>
          <span class="value">{{ customer?.name }}</span>
        </div>
        <div class="info-item">
          <span class="label">宠物名:</span>
          <span class="value">{{ customer?.petName }}</span>
        </div>
        <div class="info-item">
          <span class="label">当前余额:</span>
          <span class="value balance">¥{{ customer?.balance }}</span>
        </div>
      </div>

      <!-- 充值信息 -->
      <div class="recharge-form">
        <div class="form-item">
          <span class="label">充值金额:</span>
          <a-input-number
            v-model:value="rechargeAmount"
            :min="1"
            :precision="2"
            style="width: 200px"
            placeholder="请输入充值金额"
          />
          <span class="unit">元</span>
        </div>

        <div class="form-item">
          <span class="label">支付方式:</span>
          <a-select
            v-model:value="paymentMethod"
            style="width: 200px"
            placeholder="请选择支付方式"
          >
            <a-select-option value="cash">现金</a-select-option>
            <a-select-option value="wechat">微信支付</a-select-option>
            <a-select-option value="alipay">支付宝</a-select-option>
            <a-select-option value="card">银行卡</a-select-option>
          </a-select>
        </div>

        <div class="form-item">
          <span class="label">操作人员:</span>
          <a-select
            v-model:value="operator"
            style="width: 200px"
            placeholder="请选择操作人员"
          >
            <a-select-option value="曾前台">曾前台</a-select-option>
            <a-select-option value="李销售">李销售</a-select-option>
            <a-select-option value="王销售">王销售</a-select-option>
          </a-select>
        </div>

        <div class="form-item">
          <span class="label">备注:</span>
          <a-textarea
            v-model:value="remarks"
            :rows="3"
            style="width: 200px"
            placeholder="请输入备注信息"
          />
        </div>
      </div>

      <!-- 充值后余额预览 -->
      <div class="balance-preview">
        <div class="preview-item">
          <span class="label">充值后余额:</span>
          <span class="value">¥{{ newBalance }}</span>
        </div>
      </div>

      <!-- 操作按钮 -->
      <div class="modal-actions">
        <a-button @click="handleCancel">取消</a-button>
        <a-button type="primary" @click="handleConfirm"> 确认充值 </a-button>
      </div>
    </div>
  </a-modal>
</template>

<style scoped>
.recharge-modal {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.customer-info {
  padding: 16px;
  background: #f8f9fa;
  border: 1px solid #e8e8e8;
  border-radius: 6px;
}

.info-item {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
}

.info-item:last-child {
  margin-bottom: 0;
}

.info-item .label {
  width: 100px;
  font-size: 14px;
  color: #666;
}

.info-item .value {
  font-weight: bold;
  color: #333;
}

.info-item .balance {
  color: #ff4d4f;
}

.recharge-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.form-item {
  display: flex;
  gap: 12px;
  align-items: flex-start;
}

.form-item .label {
  width: 100px;
  font-size: 14px;
  line-height: 32px;
  color: #666;
}

.form-item .unit {
  font-size: 14px;
  line-height: 32px;
  color: #666;
}

.balance-preview {
  padding: 16px;
  background: #e6f7ff;
  border: 1px solid #91d5ff;
  border-radius: 6px;
}

.preview-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.preview-item .label {
  font-size: 14px;
  color: #666;
}

.preview-item .value {
  font-size: 18px;
  font-weight: bold;
  color: #1890ff;
}

.modal-actions {
  display: flex;
  gap: 12px;
  justify-content: flex-end;
  padding-top: 16px;
  border-top: 1px solid #e8e8e8;
}
</style>
